package com.tmptSource.service.impl;

import com.tmpt.dto.Result;
import com.tmpt.utils.DateTool;
import com.tmpt.service.impl.DataLimitService;
import com.tmpt.utils.StringUtils;
import com.tmptSource.bean.A2user;
import com.tmptSource.mapper.A2userMapper;
import com.tmptSource.mapper.OwrDoubleTaskPairMapper;
import com.tmptSource.bean.OwrDoubleTaskPair;
import com.tmptSource.dto.OwrDoubleTaskPairDto;
import com.tmptSource.dto.PageDto;
import com.tmptSource.mapper.OwrEntityListInfoMapper;
import com.tmptSource.service.intf.IOwrDoubleTaskPairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tmptSource.utils.dozer.MyMapper;
import com.tmptSource.utils.page.MyPageUtils;
import com.tmptSource.bean.OwrEntityListInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.dozer.Mapper;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.apache.log4j.Logger;

/**
 * Created by wxy on 2016/7/28.
 */
@Service(value="owrDoubleTaskPairService")
@Transactional(propagation = Propagation.REQUIRED)
public class OwrDoubleTaskPairService implements IOwrDoubleTaskPairService {

    private Logger log = Logger.getLogger(this.getClass());
    @Autowired private DataLimitService dataLimitService;
    @Autowired private Mapper mapper;
    @Autowired private MyMapper myMapper;
    @Autowired private MyPageUtils myPageUtils;
    @Autowired
    private OwrDoubleTaskPairMapper owrDoubleTaskPairMapper;
    @Autowired
    private OwrEntityListInfoMapper owrEntityListInfoMapper;
    @Autowired
    private A2userMapper a2userMapper;


    @Override
    public Result<OwrDoubleTaskPairDto> pagination(OwrDoubleTaskPairDto owrDoubleTaskPairDto,PageDto pageDto) {
        //分页查询
        Result<OwrDoubleTaskPairDto> result = myPageUtils.pagination(
                owrDoubleTaskPairDto,pageDto,
                OwrDoubleTaskPair.class,
                OwrDoubleTaskPairDto.class,
                owrDoubleTaskPairMapper,
                false,null);

        return result;
    }

    /**
    * 分布查询，有数据边界控制功能
    * @param owrDoubleTaskPairDto
    * @param pageDto
    * @param paramMap
    * @return
    */
    @Override
    public Result<OwrDoubleTaskPairDto> pagination(OwrDoubleTaskPairDto owrDoubleTaskPairDto,PageDto pageDto,Map<String,Object> paramMap) {
        //数据边界控制参数
        Map<String, Object> paramMap1 = dataLimitService.getLimitParam(paramMap);

        //分页查询
        Result<OwrDoubleTaskPairDto> result = myPageUtils.pagination(
                owrDoubleTaskPairDto, pageDto,
                OwrDoubleTaskPair.class,
                OwrDoubleTaskPairDto.class,
                owrDoubleTaskPairMapper,
                paramMap1,
                false, null);

        return result;
    }


    @Override
    public Result<OwrDoubleTaskPairDto> selectAll() {
        Result<OwrDoubleTaskPairDto> result = new Result();

        //排序
        PageDto pageDto = new PageDto();
        pageDto.setSortColumn("id");
        pageDto.setSortOrder("desc");
        Map<String,Object> mapParam = new HashMap<>();
        mapParam.put("page",pageDto);

        List<OwrDoubleTaskPair> list = owrDoubleTaskPairMapper.selectAll(mapParam);
        List<OwrDoubleTaskPairDto> dtoList = myMapper.map(list, OwrDoubleTaskPairDto.class);
        result.setResultList(dtoList);

        return result.success();
    }

    @Override
    public Result<OwrDoubleTaskPairDto> selectById(long id) {
        Result<OwrDoubleTaskPairDto> result = new Result();
        OwrDoubleTaskPair owrDoubleTaskPair = owrDoubleTaskPairMapper.selectById(id);
        OwrDoubleTaskPairDto dto = mapper.map(owrDoubleTaskPair,OwrDoubleTaskPairDto.class);
        result.setResultObj(dto);
        return result.success();
    }


    /**
        返回ID值
    */
    @Override
    public Result<OwrDoubleTaskPairDto> addData(OwrDoubleTaskPairDto owrDoubleTaskPairDto){
        Result<OwrDoubleTaskPairDto> result = new Result();

        //封闭数据
        OwrDoubleTaskPair owrDoubleTaskPair = mapper.map(owrDoubleTaskPairDto, OwrDoubleTaskPair.class);
        owrDoubleTaskPair.setCreatedate(DateTool.getCurTimestamp());

        //调用mapper
        long i = owrDoubleTaskPairMapper.insert(owrDoubleTaskPair);
        if(i==0){
            return result.fault("插入失败");
        }

        OwrDoubleTaskPairDto dto = mapper.map(owrDoubleTaskPair,OwrDoubleTaskPairDto.class);
        result.setResultObj(dto);

        return result.success();
    }

    /**
        返回ID值
    */
    @Override
    public Result<OwrDoubleTaskPairDto> editData(OwrDoubleTaskPairDto owrDoubleTaskPairDto){
        Result<OwrDoubleTaskPairDto> result = new Result();

        OwrDoubleTaskPair owrDoubleTaskPair = owrDoubleTaskPairMapper.selectById(Integer.parseInt(owrDoubleTaskPairDto.getId()));
        //封装数据
        owrDoubleTaskPair.setModifymanId(Integer.parseInt(owrDoubleTaskPairDto.getModifymanId()));
        owrDoubleTaskPair.setModifyman(owrDoubleTaskPairDto.getModifyman());
        owrDoubleTaskPair.setCompanyId(Integer.parseInt(owrDoubleTaskPairDto.getCompanyId()));
        owrDoubleTaskPair.setModifydate(DateTool.getCurTimestamp());
        String userIds = owrDoubleTaskPairDto.getUserId().replaceAll(" +"," ");
        owrDoubleTaskPair.setUserId(userIds);

        StringBuilder sb = new StringBuilder();
        String[] userIdStr = userIds.split(",");
        for (int i = 0; i < userIdStr.length; i++) {
            String idStr = userIdStr[i].trim();
            Long userIdLng = Long.parseLong(idStr);
            A2user a2user = a2userMapper.selectById(userIdLng);
            String userName = a2user.getName();
            sb.append(userName);
            if((i+1)< userIdStr.length){
                sb.append(",");
            }

        }
        owrDoubleTaskPair.setUserName(sb.toString());
        //调用mapper
        long i = owrDoubleTaskPairMapper.updateById(owrDoubleTaskPair);
        if(i==0){
            return result.fault("编辑数据失败");
        }

        OwrDoubleTaskPairDto dto = mapper.map(owrDoubleTaskPair,OwrDoubleTaskPairDto.class);
        result.setResultObj(dto);

        return result.success();
    }
    public static void main(String args[]) {
        //使用  replaceAll(" +"," ") 匹配一个或多个空格，然后替换成一个空格
        String s1 = "    SELECT     ID,NAME,   CODE FROM    TABLE1 WHERE ID = '0101'    ";
        s1 = s1.replaceAll(" +"," ");
        System.out.println("s1: "+ s1);

        //replace则不行
        String s2 = "    SELECT     ID,NAME,   CODE FROM    TABLE1 WHERE ID = '0101'    ";
        s2 = s2.replace(" +", " ");
        System.out.println("s2: " + s2);
    }

    /**
    伪删除记录，返回ID值
    */
    @Override
    public Result deleteById(long id,String adminMan){
        Result result = new Result();

        Map<String,Object> mapParameter = new HashMap();
        mapParameter.put("id",id);
        mapParameter.put("modifyman",adminMan);
        mapParameter.put("modifydate",DateTool.getCurTimestamp());

        //调用mapper
        long i = owrDoubleTaskPairMapper.deleteById(mapParameter);
        if(i==0){
            return result.fault("删除数据失败");
        }

        return result.success();
    }




    /**
    *页面上查询列的值，动态加载，sql语句读数据库配置而来
    */
    @Override
    public java.util.List select4owrRegionId(){
        return owrDoubleTaskPairMapper.select4owrRegionId();
    }
    /**
    *页面上查询列的值，动态加载，sql语句读数据库配置而来
    */
    @Override
    public java.util.List select4userId(){
        return owrDoubleTaskPairMapper.select4userId();
    }
    /**
    *页面上查询列的值，动态加载，sql语句读数据库配置而来
    */
    @Override
    public java.util.List select4entityId(){
        return owrDoubleTaskPairMapper.select4entityId();
    }

}
